package com.punai.monthanalysis.service.impl;

import java.math.BigDecimal;
import java.util.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.multi.ListValueMap;
import com.punai.common.core.domain.BaseEntity;
import com.punai.common.utils.NumberUtil;
import com.punai.externalccb.domain.YdCbfeeiteminfoStandard;
import com.punai.externalccb.mapper.YdCbfeeiteminfoStandardMapper;
import com.punai.monthanalysis.bo.ProYdcbfxGcjdkhcldbfxbBo;
import com.punai.monthanalysis.sqliteDao.CostStructureCategoryRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.punai.monthanalysis.mapper.ProYdcbfxGcjdkhcldbfxbMapper;
import com.punai.monthanalysis.domain.ProYdcbfxGcjdkhcldbfxb;
import com.punai.monthanalysis.service.IProYdcbfxGcjdkhcldbfxbService;

/**
 * 工程阶段考核材料对比分析Service业务层处理
 *
 * @author zxw_
 * @date 2023-04-19
 */
@Service
public class ProYdcbfxGcjdkhcldbfxbServiceImpl extends ServiceImpl<ProYdcbfxGcjdkhcldbfxbMapper,ProYdcbfxGcjdkhcldbfxb> implements IProYdcbfxGcjdkhcldbfxbService
{
    @Autowired
    private ProYdcbfxGcjdkhcldbfxbMapper proYdcbfxGcjdkhcldbfxbMapper;
    @Autowired
    private YdCbfeeiteminfoStandardMapper ydCbfeeiteminfoStandardMapper;
    @Autowired
    private CostStructureCategoryRepository costStructureCategoryRepository;

    @Override
    public List<? extends BaseEntity> getTabsDataList(String proId,String monthId, List<YdCbfeeiteminfoStandard> list,List<YdCbfeeiteminfoStandard> categorysList) {
        ListValueMap<String,YdCbfeeiteminfoStandard> listValueMap = new ListValueMap<>();
        list.forEach(ydCbfeeiteminfoStandard -> {
            listValueMap.putValue(ydCbfeeiteminfoStandard.getOpid()+"",ydCbfeeiteminfoStandard);
        });
        categorysList.forEach(ydCbfeeiteminfoStandard -> {
            listValueMap.putValue(ydCbfeeiteminfoStandard.getOpid()+"",ydCbfeeiteminfoStandard);
        });
        List<YdCbfeeiteminfoStandard> roots = listValueMap.get("0");
        List<ProYdcbfxGcjdkhcldbfxbBo> result = new ArrayList<>();

        if (CollUtil.isNotEmpty(roots)){
            for (YdCbfeeiteminfoStandard root : roots) {
                ProYdcbfxGcjdkhcldbfxbBo convert = convert(root);
                result.add(convert);
                setChildren(convert,listValueMap);
            }
        }
        return result;

        //按 合并单元格
//        List<YdCbfeeiteminfoStandardBo> resList = new ArrayList<>();
//        if (CollUtil.isEmpty(list)) return null;
//
//        Map<Integer, List<YdCbfeeiteminfoStandard>> mapByOid = new HashMap<>();
//        if (CollUtil.isNotEmpty(categorysList)){
//            mapByOid = categorysList.stream().filter(v -> v.getOid() != null).collect(Collectors.groupingBy(YdCbfeeiteminfoStandard::getOid, Collectors.toList()));
//        }
//        Map<Integer, List<YdCbfeeiteminfoStandard>> mapByOpid = list.stream().filter(v -> v.getOpid() != null).collect(Collectors.groupingBy(YdCbfeeiteminfoStandard::getOpid, Collectors.toList()));
//        for (Map.Entry<Integer, List<YdCbfeeiteminfoStandard>> entry : mapByOpid.entrySet()) {
//            List<YdCbfeeiteminfoStandard> standardList = mapByOid.get(entry.getKey());
//            String name = "";
//            if (CollUtil.isNotEmpty(standardList)){
//                name = standardList.get(0).getName();
//            }
//            for (YdCbfeeiteminfoStandard standard : entry.getValue()) {
//                YdCbfeeiteminfoStandardBo bo = new YdCbfeeiteminfoStandardBo();
//                BeanUtils.copyProperties(standard,bo);
//                bo.setCategoryName(name);
//                resList.add(bo);
//            }
//        }
//        return ProYdcbfxGcjdkhcldbfxbBo.convertStandard(resList);
    }

    private void setChildren(ProYdcbfxGcjdkhcldbfxbBo parent,
                             ListValueMap<String, YdCbfeeiteminfoStandard> listValueMap) {

        String id = parent.getId();
        List<YdCbfeeiteminfoStandard> children = listValueMap.get(id);
        if (CollUtil.isNotEmpty(children)) {
            for (YdCbfeeiteminfoStandard child : children) {
                ProYdcbfxGcjdkhcldbfxbBo convert = convert(child);
                parent.getChildren().add(convert);
                setChildren(convert,listValueMap);
            }
        }
    }

    private ProYdcbfxGcjdkhcldbfxbBo convert(YdCbfeeiteminfoStandard in) {
        ProYdcbfxGcjdkhcldbfxbBo out = new ProYdcbfxGcjdkhcldbfxbBo();
        out.setId(in.getOid()+"");
        out.setProId(in.getProId());
        out.setQc(in.getYdDate());//期次Id
        out.setItemDetailName(in.getName());
        out.setUnit(in.getUnit());

        BigDecimal ysTotal = in.getMonthCzTotal();
        out.setYsQuantity(in.getMonthAmount());
        out.setYsAvgPrice(in.getMonthPrice());
        out.setYsPrice(in.getMonthCzTotal());

        out.setSjQuantity(in.getActualCostAmout());
        out.setSjAvgPrice(in.getActualCostNoTaxPrice());
        out.setSjPrice(in.getActualCostNoTaxTotal());

        BigDecimal jce = in.getKlDifferenceOfCzsjTotal();
        BigDecimal jcl = NumberUtil.divideNew(jce, 2, ysTotal);
        out.setJcQuantity(in.getDifferenceOfCzsjAmount());
        out.setJce(in.getKlDifferenceOfCzsjTotal());
        out.setJcl(jcl);
        out.setJcReason(in.getMainReason());
        return out;
    }
}
